package com.gzwanchuang.xhgj.ui.page.home;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.gzwanchuang.xhgj.R;
import com.gzwanchuang.xhgj.bridge.request.HomeRequestViewModel;
import com.gzwanchuang.xhgj.bridge.state.home.ShuaKaViewModel;
import com.gzwanchuang.xhgj.databinding.FragmentShuaKaBinding;
//import com.helipay.mposlib.api.MPApi;
//import com.helipay.mposlib.api.MPApiError;
//import com.helipay.mposlib.api.MPIConsumeCallback;
//import com.helipay.mposlib.api.MPPosResultModel;
//import com.helipay.mposlib.api.MPRequestParams;

/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class ShuaKaFragment extends com.gzwanchuang.xhgj.ui.base.BaseFragment {
    private FragmentShuaKaBinding mBinding;
    private ShuaKaViewModel mViewModel;
    private HomeRequestViewModel mRequestViewModel;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(ShuaKaViewModel.class);
        int type = getArguments().getInt("type",0);
        if (type == 1) // ViP
        {
            mViewModel.title.set("VIP收款");
            mViewModel.max = 9999;
            mViewModel.message.set("单笔交易金额10-"+ mViewModel.max + "元");
        }
        mRequestViewModel = ViewModelProviders.of(this).get(HomeRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shua_ka, container, false);
        mBinding = FragmentShuaKaBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRequestViewModel.getErrorString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });
        mRequestViewModel.getResultString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });

        mRequestViewModel.getBank().observe(this, bank -> {
            hideProgress();
            mViewModel.bean.set(bank);
            mViewModel.image.set(bank.getLogo());
            mViewModel.name.set(bank.getBankName() + "(" + bank.getCard().substring(bank.getCard().length() - 4) + ")");
        });

        mRequestViewModel.getOrder().observe(this, created_order -> {
            hideProgress();
            mViewModel.order = created_order;
            createShuaKa();
        });
    }

    @Override
    public void loadInitData() {
        super.loadInitData();
        showProgress();
        mRequestViewModel.get_bank("1", "2");
    }
    private void createShuaKa() {
//        MPRequestParams  params = new MPRequestParams();
//        params. setShowBindTerminalPrompt (true);// 是否首次绑定提示（默认提示）
//        params.setAmount(mViewModel.money.get());
//        params.setAgentNo(mViewModel.order.getAgentNo());
//        params.setUserId(mViewModel.order.getUserId());
//        params.setAgentOrderId(mViewModel.order.getOrderId());
//        params.setSplitRule(mViewModel.order.getSplitRule());
//        params.setDescription(mViewModel.order.getRequest());
//        params.setMerchantId(mViewModel.order.getMerchantId());
//        MPApi.INSTANCE.startConsume(mActivity, params, new MPIConsumeCallback() {
//
//            @Override
//            public void onSuccess(MPPosResultModel model, Object... expandDatas) {
//                Toast.makeText(mActivity, "收款成功：", Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onFailure(MPApiError error, Object... expandDatas) {
//                Toast.makeText(mActivity, "收款失败：" + error.getErrorInfo(), Toast.LENGTH_SHORT).show();
//            }
//        });
    }


    public class ClickProxy {
        public void back() {
            nav().navigateUp();
        }

        public void submit() {
            String monye = mViewModel.money.get();
            if (TextUtils.isEmpty(monye)) {
                showShortToast("请输入金额");
            } else {
                Float aFloat = Float.valueOf(monye);
                if (aFloat < 10) {
                    showShortToast("刷卡金额最低10元");
                    return;
                }
                if (aFloat > mViewModel.max) {
                    showShortToast("刷卡金额最高" + mViewModel.max + "元");
                    return;
                }
                mRequestViewModel.created_order();
            }

        }


    }
}
